import unittest

from PyQt4 import QtCore, QtGui

from limber.ui import tools as uitools
from tools import ConnectionBox



class MockWidget(QtCore.QObject):

    def __init__(self):
        super(MockWidget, self).__init__(None)
        self._actions = []
        self._separators = []

    def addAction(self, action):
        if action.isSeparator():
            self._separators.append(action)
        else:
            self._actions.append(action)


class MockAction(object):
    def __init__(self, text):
        self._text = text
        self.is_separator = False

    def setSeparator(self, sep):
        self.is_separator = bool(sep)

    def isSeparator(self):
        return self.is_separator


class TestUiTools(unittest.TestCase):

    def testCreateAction(self):
        parent = None
        text = 'Action'
        checkable = False
        status_tip =  'a status tip'
        tool_tip = 'a tool tip'
        whats_this = 'a whats this'
        shortcut = 'Ctrl+D'
        signal = 'triggered()'
        data_text = 'testing data'

        box = ConnectionBox()
        action = uitools.create_action(parent, text,
                                       slot=box.slot, signal=signal,
                                       checkable=checkable,
                                       status_tip=status_tip,
                                       tool_tip=tool_tip,
                                       whats_this=whats_this,
                                       shortcut=shortcut,
                                       data=data_text)

        self.assertTrue(isinstance(action, QtGui.QAction))

        self.assertEqual(unicode(action.text()), text)
        self.assertEqual(unicode(action.statusTip()), status_tip)
        self.assertEqual(unicode(action.toolTip()), tool_tip)
        self.assertEqual(unicode(action.whatsThis()), whats_this)
        self.assertEqual(unicode(action.shortcut().toString()), shortcut)
        self.assertEqual(unicode(action.data().toString()), data_text)
        self.assertEqual(action.parent(), parent)
        self.assertEqual(action.isCheckable(), checkable)

        action.trigger()
        action.trigger()
        self.assertEqual(box.signals_arrived, 2)

    def testCreateActionPropagateTips(self):
        kwargs = {
            'parent' : None,
            'text' : 'text',
            'tool_tip' : 'the tool tip',
            'propagate_tips' : True,
            }

        action = uitools.create_action(**kwargs)
        self.assertEqual(action.toolTip(), action.statusTip())
        self.assertEqual(action.whatsThis(), action.statusTip())
        self.assertEqual(action.toolTip(), kwargs['tool_tip'])

    def testCreateActionHasData(self):
        kwargs = {
            'parent' : None,
            'text' : 'text',
            'data' : None,
            'has_data' : True,
            }

        action = uitools.create_action(**kwargs)
        self.assertEqual(action.data().toPyObject(), None)

    def testAddActions(self):
        widget = MockWidget()
        actions = [
            MockAction('a'),
            MockAction('b'),
            MockAction('c'),
            None,
            ]

        uitools.add_actions(widget, actions)

        self.assertEqual(widget._actions, actions[:-1])
        self.assertTrue(widget._separators[-1].isSeparator())